﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Data;
namespace DevExpress.AgDataGrid {
	public enum AgDataGridCellState { Normal, Focused, FocusedRow, Editing };
	[TemplatePart(Name = ContentName, Type = typeof(ContentControl))]
	[TemplatePart(Name = RootElementName, Type = typeof(Grid))]
	public class AgDataGridCell : AgColumnCellBase {
		const string ContentName = "ContentElement";
		const string RootElementName = "RootElement";
		public TextWrapping TextWrapping { get; set; }
		AgDataGridCellData cachedValue;
		AgDataGridCellState state;
		Grid rootElement;
		int rowHandle;
		IAgDataGridRowOwner rowOwner;
		public AgDataGridCell(AgDataGridColumn column, IAgDataGridRowOwner rowOwner, int rowHandle)
			: base(column) {
			DefaultStyleKey = typeof(AgDataGridCell);
			this.rowOwner = rowOwner;
			this.rowHandle = rowHandle;
			UpdateContent();
			SetStyle();
		}
		public int RowHandle {
			get {
				return rowHandle;
			}
			set {
				if (rowHandle == value) return;
				rowHandle = value;
				OnChangeRowHandle();
			}
		}
		private void OnChangeRowHandle() {
			UpdateContent();
		}
		public AgDataGridCellData Value { get { return RowOwner.GetCellValue(RowHandle, Column.FieldName, ref cachedValue); } }
		public DataTemplate ColumnTemplate { get { return Column.GetCellDisplayTemplate(); } }
		protected int Level { get { return RowOwner.GetLevel(RowHandle); } }
		protected double Indent { get { return Column.VisibleIndex == 0 ? Level * AgDataGrid.DefaultLevelIndent : 0; } }
		protected IAgDataGridRowOwner RowOwner { get { return rowOwner; } }
		protected Grid RootElement { get { return rootElement; } }
		public Rect Bounds {
			get {
				UIElement parent = Parent as UIElement;
				if (parent == null) return Rect.Empty;
				return new Rect(TransformToVisual(parent).Transform(new Point(0, 0)), new Point(Width, Height));
			}
		}
		public void UpdateContent() {
			UpdateContent(false);
		}
		public void UpdateContent(bool reloadContent) {
			DataContext = Value;
			try {
				if (Content == null || reloadContent)
					Content = ColumnTemplate.LoadContent();
			} catch { } 
			PrepareDisplayElement();
		}
		protected void SetStyle() {
			Column.PrepareCell(this);
		}
		protected override double ColumnWidth { get { return base.ColumnWidth - Indent; } }
		public override void OnApplyTemplate() {
			base.OnApplyTemplate();
			if (Column.Owner.IsEditorShown) return;
			CreateRootElement();
			ApplyStyle();
			PrepareDisplayElement();
		}
		private void PrepareDisplayElement() {
			if (Content != null)
				Column.OnPrepareCellDisplayElement(this, Content as FrameworkElement);
		}
		protected virtual void CreateRootElement() {
			this.rootElement = GetRootElement();
		}
		protected virtual Grid GetRootElement() {
			return (Grid)GetTemplateChild(RootElementName);
		}
		public virtual ContentControl ContentControl {
			get {
				return GetTemplateChild(ContentName) as ContentControl;
			}
		}
		public AgDataGridCellState State {
			get { return state; }
			set {
				if (state == value) return;
				state = value;
				ApplyStyle();
			}
		}
		protected virtual void ApplyStyle() {
			if (Column == null) return;
			RowOwner.ApplyCellStyle(this);
			DoPrepareDisplayElement();
		}
		protected void DoPrepareDisplayElement() {
			if (Content is FrameworkElement && !Column.Owner.IsEditorShown) {
				Column.OnPrepareCellDisplayElement(this, Content as FrameworkElement);
			}
		}
	}
}
